package com.eii.eiimq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @description:
 * @author: eii
 * @time: 2020/7/10 10:10 下午
 */
@Configuration
public class RabbitMQConfig {
    //发送生产者消息任务交换机
    public static final String EX_EIIMQ_SEND_MSG = "ex_eiimq_send_msg";
    //发送生产者消息队列
    public static final String EIIMQ_SEND_MSG_QUEUE = "eiimq_send_msg_queue";
    //发送生产者消息路由key
    public static final String EIIMQ_SEND_MSG_KEY = "eiimq_send_msg_key";


    //发送消费者消息队列
    public static final String EIIMQ_RECV_MSG_QUEUE = "eiimq_recv_msg_queue";
    //发送消费者消息路由key
    public static final String EIIMQ_RECV_MSG_KEY = "eiimq_recv_msg_key";

    /**
     * 交换机配置
     * @return the exchange
     */
    @Bean(EX_EIIMQ_SEND_MSG)
    public Exchange EX_DECLARE() {
        return ExchangeBuilder.directExchange(EX_EIIMQ_SEND_MSG).durable(true).build();
    }

    //声明队列 发送生产者消息队列
    @Bean(EIIMQ_SEND_MSG_QUEUE)
    public Queue QUEUE_DECLARE() {
        Queue queue = new Queue(EIIMQ_SEND_MSG_QUEUE,true,false,true);
        return queue;
    }

    /**
     * 绑定发送生产者消息队列到交换机 .
     * @param queue    the queue
     * @param exchange the exchange
     * @return the binding
     */
    @Bean
    public Binding binding_sendmsg_processtask(@Qualifier(EIIMQ_SEND_MSG_QUEUE) Queue queue, @Qualifier(EX_EIIMQ_SEND_MSG) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(EIIMQ_SEND_MSG_KEY).noargs();
    }

    //======================================

    //接受 消费者backmsg
    //声明队列
    @Bean(EIIMQ_RECV_MSG_QUEUE)
    public Queue QUEUE_DECLARE_2() {
        Queue queue = new Queue(EIIMQ_RECV_MSG_QUEUE,true,false,true);
        return queue;
    }

    //绑定 消息队列到交换机
    @Bean
    public Binding binding_backmsg_processtask(@Qualifier(EIIMQ_RECV_MSG_QUEUE) Queue queue, @Qualifier(EX_EIIMQ_SEND_MSG) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(EIIMQ_RECV_MSG_KEY).noargs();
    }
}
